package br.com.amadeus.android.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;
import br.com.amadeus.android.entities.Message;
import br.com.amadeus.android.historic.AmadeusDroidHistoric;


public class RESTFileContent {
			

	public InputStream getRESTFileContent(String url) throws ClientProtocolException, IOException , UnknownHostException{ 
		
		HttpResponse response;
		HttpEntity entity;
		HttpClient httpclient = new DefaultHttpClient();  
		HttpGet httpget = new HttpGet(url);
		InputStream instream = null;	
		
		response = httpclient.execute(httpget);
		entity = response.getEntity();
		if (entity != null) {  

			instream = entity.getContent();
					
		} 
					  
		return instream; 
	}
	
	public Vector<Object> getRestFileMaterial(String urlMaterial) throws ClientProtocolException, IOException , UnknownHostException{ 
		Vector<Object> vector = new Vector<Object>();
		
		HttpResponse response;
		HttpEntity entity;
		HttpClient httpclient = new DefaultHttpClient();  
		HttpGet httpget = new HttpGet(urlMaterial);
		InputStream instream = null;	
		response = httpclient.execute(httpget);
		
		int status = response.getStatusLine().getStatusCode();
		if (status == 200) {
			entity = response.getEntity();
			String data = EntityUtils.toString(entity);
			
			response = httpclient.execute(httpget);
			entity = response.getEntity();
			instream = entity.getContent();
			
			vector.add(data);
			vector.add(instream);
		}
		
		
		return vector;
	}
	
	public InputStream postRESTFileContent(String url, AmadeusDroidHistoric historic) throws ClientProtocolException, IOException{
		
		HttpResponse response;
		HttpEntity entity;
		HttpClient httpclient = new DefaultHttpClient();  
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("course_name", historic.getCourseName()));
		params.add(new BasicNameValuePair("user_name", historic.getUserName()));
		params.add(new BasicNameValuePair("action_value", historic.getActionValue()));
		params.add(new BasicNameValuePair("resource_name", historic.getResourceName()));
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		params.add(new BasicNameValuePair("creation_date", dateFormat.format(historic.getCreationDate())));
		UrlEncodedFormEntity ent =	new UrlEncodedFormEntity(params,HTTP.UTF_8);
		httpPost.setEntity(ent);
		InputStream instream = null;	
		
		response = httpclient.execute(httpPost);
		entity = response.getEntity();
		if (entity != null) {  

			instream = entity.getContent();
					
		} 
					  
		return instream; 
	}
	
	public InputStream sendForumMessageToAmadeusWeb(String url, Message message) throws ClientProtocolException, IOException{
		
		HttpResponse response;
		HttpEntity entity;
		HttpClient httpclient = new DefaultHttpClient();  
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("body", message.getBody()));
		params.add(new BasicNameValuePair("person", Integer.toString(message.getPerson_id())));
		params.add(new BasicNameValuePair("forum", Integer.toString(message.getForum().getAmadeus_id())));
		
		UrlEncodedFormEntity ent =	new UrlEncodedFormEntity(params,HTTP.UTF_8);
		httpPost.setEntity(ent);
		InputStream instream = null;
		
		response = httpclient.execute(httpPost);
		entity = response.getEntity();
		if (entity != null) {
			instream = entity.getContent();
		}
		
		return instream;
	}
	
	public static boolean testHostValidation(String url) throws ClientProtocolException, IOException{
		boolean host_valido = false;
		
		HttpClient httpclient = new DefaultHttpClient();  
		HttpGet httpget = new HttpGet(url);
		Log.i("TESTEEE", url);
		
		@SuppressWarnings("unused")
		HttpResponse respTest = httpclient.execute(httpget);
		host_valido = true;
		
		return host_valido;
	}
	
	public String toString(InputStream is) throws IOException{

		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		
		StringBuilder sb = new StringBuilder();
		String line;
    	while ((line = br.readLine()) != null) {
    		sb.append(line);
    	} 
    	String s = sb.toString();
    	br.close();
		return s;
	}

		

}
